/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimp.business.service.cargoDeclaration.mapper;

import com.mrd.gtimp.business.entity.Commodity;
import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.gtimp.business.entity.SuiFuDan;
import com.mrd.util.StringUtils;

import org.apache.commons.collections.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bean.decla.CargoDeclarationInformation;
import bean.decla.DeclaCertificate;
import bean.decla.DeclaContainer;
import bean.decla.DeclaCustoms;
import bean.decla.DeclaGood;
import bean.decla.DeclaTrn;
import bean.decla.DeclaTrnContaGood;
import bean.decla.DeclaTrnContainer;
import bean.decla.DeclaTrnHead;
import bean.decla.DeclaTrnList;
import bean.decla.EcoRelation;
import mapper.DeclarationMapper;

/**
 * Created by dengdc on 2017/5/27.
 */
public abstract class BaseDeclarationMapper implements DeclarationMapper<Declaration>{

    public CargoDeclarationInformation exec(Declaration dn) {
        if(dn == null){
            return null;
        }
        Declaration.sortCommidities(dn);
        CargoDeclarationInformation info = new CargoDeclarationInformation();
        DeclaCustoms declaCustoms = createDeclaCustoms(dn);
        info.setCustoms(declaCustoms);
        List<DeclaGood> declaGoods = createDeclaGoods(dn.getCommodities());
        info.setGoodList(declaGoods);
        List<DeclaContainer> containerList = createContainerList(dn.getSortedDeclaContainers());
        info.setContainerList(containerList);
        return info;
    }

    protected abstract DeclaCustoms createDeclaCustoms(Declaration dn);

    protected abstract List<DeclaGood> createDeclaGoods(List<Commodity> commodities);

    protected List<EcoRelation> createEcoRelations(Declaration dn){
        List<EcoRelation> ecoRelationList = new ArrayList<>();
        return ecoRelationList;
    }

    /**
     * 集装箱信息
     * @param containers
     * @return
     */
    protected List<DeclaContainer> createContainerList(List<com.mrd.gtimp.order.entity.DeclaContainer> containers){
        List<DeclaContainer> containerList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(containers)){
            for (com.mrd.gtimp.order.entity.DeclaContainer conta : containers) {
                DeclaContainer declaContainer = new DeclaContainer();
                //集装箱号
                declaContainer.setContainerId(conta.getContainerNo());
                //集装箱自重
                declaContainer.setContainerWt(conta.getContainerWeight());
                //国检集装箱规格
                declaContainer.setCiqContainerMd(null);
                //海关集装箱规格
                declaContainer.setCustomsContainerMd(conta.getContainerStandard());
                //拼箱标示 0否，1是
                declaContainer.setLclFlag("0");
                containerList.add(declaContainer);
            }
        }
        return containerList;
    }

    /**
     * 随附单信息
     * @param dn
     * @return
     */
    protected List<DeclaCertificate> createDeclaCerList(Declaration dn){
        List<DeclaCertificate> certificateList = new ArrayList<>();
        List<SuiFuDan> suiFuDans = dn.getSuiFuDans();
        if(CollectionUtils.isNotEmpty(suiFuDans)){
            for (SuiFuDan suiFuDan : suiFuDans) {
                DeclaCertificate declaCertificate = new DeclaCertificate();
                //随附单证代码
                declaCertificate.setDocuCode(suiFuDan.getSuifudan_co());
                //随附单证编号
                declaCertificate.setCertCode(suiFuDan.getSuifudan_no());
                certificateList.add(declaCertificate);
            }
        }

        return certificateList;
    }

    /**
     * 转关运输单
     * @param dn 报关单
     */
    protected DeclaTrn createTrn(Declaration dn){
        if("转关".equals(dn.getData_type())){
            DeclaTrn trn = new DeclaTrn();
            trn.setTrnHead(createTrnHead(dn));
            trn.setTrnList(createTrnList(dn));
            trn.setTrnContainerList(createTrnContainerList(dn));
            trn.setTrnContaGoodList(createTrnContaGoodsList(dn));
            return trn;
        }
        return null;
    }

    /**
     * 转关运输单
     * @param dn 报关单
     */
    protected DeclaTrnHead createTrnHead(Declaration dn){
        DeclaTrnHead trnHead = new DeclaTrnHead();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        // 转关单统一编号
        trnHead.setTrnPreId(null);
        // 载货清单
        String transportNa = dn.getTransport_na();
        String transNo = null;
        if(StringUtils.isNotEmpty(transportNa) && transportNa.contains("@")){
            transNo = transportNa.replace("@", "");
        }
        trnHead.setTransNo(transNo);
        // 转关申报单号
        trnHead.setTurnNo(null);
        // 境内运输方式
        trnHead.setNativeTrafMode(dn.getTransport_mode_code());
        // 境内运输工具编号
        trnHead.setTrafCustomsNo(null);
        // 境内运输工具名称
        trnHead.setNativeShipName(null);
        // 境内运输工具航次
        trnHead.setNativeVoyageNo(null);
        // 承运单位名称
        trnHead.setContractorName(null);
        // 承运单位组织机构代码
        trnHead.setContractorCode(null);
        // 转关类型
        trnHead.setTransFlag(null);
        // 预计运抵指运地时间
        trnHead.setValidTime(yyyyMMdd.format(new Date()));
        // 是否启用电子关锁标志
        trnHead.seteSealFlag("N");
        // 备注
        trnHead.setNotes(null);
        // 转关单类型
        trnHead.setTrnType("0");
        return trnHead;
    }

    /**
     * 提运单信息
     */
    protected DeclaTrnList createTrnList(Declaration dn){
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        DeclaTrnList trnList = new DeclaTrnList();
        // 进出境运输方式
        trnList.setTrafMode("1");
        // 进出境运输工具编号
        trnList.setShipId(null);
        // 进出境运输工具名称
        trnList.setShipName(null);
        // 进出境运输工具航次
        trnList.setVoyageNo(null);
        // 提单号
        trnList.setBillNo(dn.getBl_no());
        // 实际进出境日期
        Date exportDate = dn.getExport_date();
        String exportDateStr = null;
        if(exportDate != null){
            exportDateStr = yyyyMMdd.format(exportDate);
        }
        trnList.setIeDate(yyyyMMdd.format(new Date()));
        return trnList;
    }

    protected List<DeclaTrnContainer> createTrnContainerList(Declaration dn){
        List<com.mrd.gtimp.order.entity.DeclaContainer> containers = dn.getSortedDeclaContainers();
        List<DeclaTrnContainer> trnContainerList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(containers)){
            int i = 1;
            for (com.mrd.gtimp.order.entity.DeclaContainer container : containers) {
                DeclaTrnContainer trnContainer = new DeclaTrnContainer();
                trnContainer.setContaNo(container.getContainerNo());
                trnContainer.setContaSn(String.valueOf(i));
                i++;
                trnContainer.setContaModel(container.getContainerStandard());
                trnContainer.setSealNo(null);
                String tansName = dn.getShip_code() + "/" + dn.getShip_name();
                trnContainer.setTransName(tansName.replaceAll("/?null|null/?", ""));
                trnContainer.setTransWeight(null);
                trnContainerList.add(trnContainer);
            }
        }
        return trnContainerList;
    }

    protected List<DeclaTrnContaGood> createTrnContaGoodsList(Declaration dn){
        List<DeclaTrnContaGood> trnContaGoodsList = new ArrayList<>();
//        List<com.mrd.gtimp.order.entity.DeclaContainer> containers = dn.getDeclaContainers();
//        if(CollectionUtils.isNotEmpty(containers)){
//            int i = 1;
//            for (com.mrd.gtimp.order.entity.DeclaContainer container : containers) {
//                DeclaTrnContaGood declaTrnContaGood = new DeclaTrnContaGood();
//                declaTrnContaGood.setContaNo(container.getContainerNo());
//                declaTrnContaGood.setContaGoodsCount(null);
//                declaTrnContaGood.setContaGoodsWeight(null);
//                declaTrnContaGood.setgNo(String.valueOf(i));
//                i++;
//                trnContaGoodsList.add(declaTrnContaGood);
//            }
//        }
        return trnContaGoodsList;
    }



    /**
     * 将文字的报关单类型转换为代码
     * @param customEntryType
     * @return
     */
    protected String turnEntryType(String customEntryType){
        if(StringUtils.isEmpty(customEntryType)){
            return null;
        }
        String entryType = "";
        switch (customEntryType){
            case "通关无纸化":
                entryType = "M";
                break;
            case "有纸报关":
                entryType = "O";
                break;
            default:
        }
        return entryType;
    }

    /**
     * 将文字的成交方式转换为代码
     * @param customTransMode
     */
    protected String turnTransMode(String customTransMode){
        if(StringUtils.isEmpty(customTransMode)){
            return null;
        }
        String tansMode = "";
        switch (customTransMode){
            case "CIF":
                tansMode = "1";
                break;
            case "C&F":
                tansMode = "2";
                break;
            case "FOB":
                tansMode = "3";
                break;
            case "C&I":
                tansMode = "4";
                break;
            case "市场价":
                tansMode = "5";
                break;
            case "垫仓":
                tansMode = "6";
                break;
            default:
        }
        return tansMode;
    }

    protected String turnPackageType(String customsPkgType){
        if(StringUtils.isEmpty(customsPkgType)){
            return null;
        }
        String pkgType = "";
        switch (customsPkgType){
            case "木箱":
                pkgType = "1";
                break;
            case "纸箱":
                pkgType = "2";
                break;
            case "桶装":
                pkgType = "3";
                break;
            case "散装":
                pkgType = "4";
                break;
            case "托盘":
                pkgType = "5";
                break;
            case "包":
                pkgType = "6";
                break;
            case "其它":
                pkgType = "7";
                break;
            default:
        }
        return pkgType;
    }

    /**
     * 将集装箱代码正常海关代码
     * @param customsContainerStanderd
     * @return
     */
    protected String turnContainerStanderd(String customsContainerStanderd){
        if(StringUtils.isEmpty(customsContainerStanderd)){
            return null;
        }
        String containerStanderd = "";
        switch (customsContainerStanderd){
            case "L":
                containerStanderd = "111";
                break;
            case "S":
                containerStanderd = "211";
                break;
            default:
        }
        return containerStanderd;
    }

    /**
     * 01互转
     * @return
     */
    protected String revert01(String param){
        if(param == null){
            return "";
        }
        switch (param){
            case "1":
                return "0";
            case "0":
                return "1";
            default:
                return param;
        }
    }

    protected String turnDeclaType(String customDeclaType){
        if(StringUtils.isEmpty(customDeclaType)){
            return null;
        }
        String declaType = "";
        switch (customDeclaType){
            case "直航":
                declaType = "001";
                break;
            case "转关":
                declaType = "003";
                break;
            default:
        }
        return declaType;
    }

    /**
     * 中文费用单位转代码
     * @param customFeeType
     */
    protected String turnFeeType(String customFeeType){
        if(StringUtils.isEmpty(customFeeType)){
            return null;
        }
        String feeTypeCode = "";
        switch (customFeeType){
            case "率":
                feeTypeCode = "1";
                break;
            case "总价":
                feeTypeCode = "3";
                break;
            default:
        }
        return feeTypeCode;
    }
}
